<?php

namespace App\Game;

use App\Models\Def\MainGamePlat;
use App\Models\OddsHandicap;
use Carbon\Carbon;
use App\Game\Traits\GgTrait;

class gg extends Game
{
    use GgTrait;

//    public $timezone = 'PRC';
    private $platName = 'gg';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1,
            'bet_flow_available' => 1,
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }



    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $data = [
            'action' => 1000,
            'data' => [
                'acc' => $accountUserName,
                'params' => [
                    'cn' => 'agent001',
                ],
            ],
        ];
        $output = $this->ggRequest('GET', $data);
        $outputarr = json_decode($output, true);
        if (!isset($outputarr['error']) || $outputarr['error'] == 0) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }


    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $data = [
            'action' => 1003,
            'data' => [
                'acc' => $accountUserName,
            ],
        ];
        $output = $this->ggRequest('GET', $data);
        $outputarr = json_decode($output, true);
        if (!isset($outputarr['error']) || $outputarr['error'] == 0) {
            return ['success' => true, 'data' => ['balance' => $outputarr['result']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询失败'];
        }
    }

    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $prepareResult = $this->prepareTransfer($accountUserName, 3000, $price, $transferid);
        $confirmResult = $this->transferConfirm($accountUserName, 3002, $transferid);

        if ($prepareResult['success'] && $confirmResult['success']) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } elseif (!$prepareResult['success']) {
            return $prepareResult;
        } elseif (!$confirmResult['success']) {
            return $confirmResult;
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $prepareResult = $this->prepareTransfer($accountUserName, 3001, $price, $transferid);
        $confirmResult = $this->transferConfirm($accountUserName, 3002, $transferid);

        if ($prepareResult['success'] && $confirmResult['success']) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } elseif (!$prepareResult['success']) {
            return $prepareResult;
        } elseif (!$confirmResult['success']) {
            return $confirmResult;
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $this->initHeader();
        $url = $this->gameUrl . '/v1/payments/' . $request->transferid;
        $data = [
            'operationType' => 'transfer',
        ];

        $output = $this->request('GET', $url, $data, $this->header);
        $outputarr = json_decode($output, true);

        if ($outputarr['status'] == 'committed') {
            return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '查询转账失败'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    //进入游戏
    public function curlJoinGame($request)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = OddsHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $playerOdds = isset($oddType) ? $oddType->odds_value : '';  //赔率
        $data = [
            'action' => 1001,
            'data' => [
                'acc' => $request->input('accountUserName'),
                'pf' => 'pc',
                'level' => $playerOdds,
                'params' => [
                    'cn' => 'agent001',
                ],
            ],
        ];
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'ggstart...'.json_encode($data).PHP_EOL, FILE_APPEND);
        $output = $this->ggRequest('GET', $data);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'ggstart...'.$output.PHP_EOL, FILE_APPEND);
        $outputarr = json_decode($output, true);
        if (!isset($outputarr['error']) || $outputarr['error'] == 0) {
            $url_arr = $outputarr['result'];
            $url = $url_arr['url'].'?user_id='.$url_arr['user_id'].'&token='.$url_arr['token'];
            return ['success' => true, 'data' => ['url' => $url], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }


    public function curlJoinMobileGame($request)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = OddsHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $playerOdds = isset($oddType) ? $oddType->odds_value : '';  //赔率
        $data = [
            'action' => 1001,
            'data' => [
                'acc' => $request->input('accountUserName'),
                'pf' => 'mobile',
                'level' => $playerOdds,
                'params' => [
                    'cn' => 'agent001',
                ],
            ],
        ];
        $output = $this->ggRequest('GET', $data);
        $outputarr = json_decode($output, true);
        if (!isset($outputarr['error']) || $outputarr['error'] == 0) {
            $url_arr = $outputarr['result'];
            $url = $url_arr['url'].'?user_id='.$url_arr['user_id'].'&token='.$url_arr['token'];
            return ['success' => true, 'data' => ['url' => $url], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'ggstart...'.PHP_EOL, FILE_APPEND);
        $datas = [
            'action' => 2000,
            'data' => [
                'max' => 50,
            ],
        ];
        $del_datas = [
            'action' => 2001,
            'data' => [
                'list' => [],
            ],
        ];
        do {
            $output = $this->ggRequest('GET', $datas);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'ggsa...'.$output.PHP_EOL, FILE_APPEND);
            $outputarr = json_decode($output, true);
            if (!empty($outputarr['result'])) {
                $data = array_merge($data, $outputarr['result']);
                //删除掉订单
                $del_datas['data']['list'] = array_column($outputarr['result'], 'id');
                //记录被删掉的id
                \Log::info('GG彩票确认id', $del_datas['data']['list']);
                $this->ggRequest('POST', $del_datas);
                sleep(2);
            } else {
                break;
            }
        } while (true);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'gg...'.json_encode($data).PHP_EOL, FILE_APPEND);
        return $data;
    }


}
